Skip to content

Conversation

@mtfishman
Copy link
Collaborator

@mtfishman mtfishman commented Apr 9, 2025

This is a work in progress for allowing more general truncation and algorithm selection in orth/null. So far I've only worked on left_orth! so I can get feedback on the design. Also some of the tests will depend on #18 getting merged in order to work with the new design.

@lkdvos and @Jutho, I'm curious to hear your thoughts, particularly related to the design for selecting the algorithms of different factorizations.

The basic idea is that you can select an algorithm or a set of keyword arguments for the default algorithm of each factorization separately, for example:

using MatrixAlgebraKit

A = randn(4, 4)

# QR will be selected, in which case the `blocksize` will be used and `alg_svd` will be ignored.
V, C = left_orth(A; alg_qr=(; blocksize=2), alg_svd=LAPACK_DivideAndConquer())

# Truncated SVD will be selected, in which case the truncation and specified SVD algorithm will be used and the `blocksize` will be ignore.
V, C = left_orth(A; trunc=(; maxrank=2), alg_qr=(; blocksize=2), alg_svd=LAPACK_DivideAndConquer())

@mtfishman mtfishman changed the title [WIP] More general truncation and algorithm selection in orth/null [RFC] More general truncation and algorithm selection in orth/null Apr 10, 2025
@Jutho
Copy link
Member

Jutho commented Apr 17, 2025

I definitely like this approach. The only thing I wonder about is if the alg_... variables become type unstable using this approach, but that is probably a negligible effect anyway.

@mtfishman
Copy link
Collaborator Author

mtfishman commented Apr 17, 2025

I definitely like this approach. The only thing I wonder about is if the alg_... variables become type unstable using this approach, but that is probably a negligible effect anyway.

I refactored the logic a bit, hopefully it is now friendlier for humans and compilers. (I can double check the type stability but it looks good so far.)

@mtfishman
Copy link
Collaborator Author

I considered having separate keyword arguments alg_qr and kwargs_qr but that seemed excessive.

@Jutho
Copy link
Member

Jutho commented Apr 17, 2025

I agree that would be excessive and like the current approach. The test breaking seems to be because this is based on an older version where rtol truncation was not yet supported.

@mtfishman
Copy link
Collaborator Author

mtfishman commented Apr 17, 2025

I agree that would be excessive and like the current approach. The test breaking seems to be because this is based on an older version where rtol truncation was not yet supported.

Yes, I just merged the changes to #18 into this branch so I'll be able to fix that now. I'll go through and update tests, add new tests, update the other orth and null functions, etc.

@codecov
Copy link

codecov bot commented Apr 17, 2025

Codecov Report

Attention: Patch coverage is 98.46154% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/implementations/orthnull.jl 98.36% 1 Missing ⚠️
Files with missing lines Coverage Δ
src/algorithms.jl 81.96% <100.00%> (+1.26%) ⬆️
src/interface/orthnull.jl 66.66% <ø> (ø)
src/implementations/orthnull.jl 96.77% <98.36%> (-0.93%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@mtfishman mtfishman marked this pull request as ready for review April 17, 2025 20:41
@mtfishman mtfishman changed the title [RFC] More general truncation and algorithm selection in orth/null More general truncation and algorithm selection in orth/null Apr 17, 2025
@mtfishman
Copy link
Collaborator Author

mtfishman commented Apr 17, 2025

Ok, this PR is ready for a review, I've updated left_orth, right_orth, left_null, and right_null, along with the docs and tests.

The most awkward part of this new design is deciding how to interpret trunc in left_null and right_null.

The latest design is that if you pass trunc=(; atol, rtol, maxnullity), these decide which small vectors to keep by building a corresponding truncation strategy. maxnullity specifies the maximum size of the null space to output, so is like the complement of maxrank.

However, if you pass a TruncationStrategy to trunc, it is taken literally, so it basically acts the same way orth would act for that TruncationStrategy (i.e. it would keep the same singular vectors as if you passed that trunc to svd_full). I see now that's why the previous design may have been more restrictive about the inputs for the truncation.

The choices I see are:

  1. The current design is fine, and it is up to users to pass a good truncation strategy that selects some part of the null space.
  2. The truncation strategy could be run on the singular values and then the complement of the ones that are filtered would be used. That seems a bit too tricky/magical.
  3. We only allow a NamedTuple to be passed as trunc, but I'm not a huge fan of that since there are reasonable alternative truncation strategies such as picking the null space based on summing the square of singular values and that would be difficult to implement without allowing a generally truncation strategy input.

Curious to hear your thoughts on that, I'm definitely open to suggestions.

Copy link
Member

@lkdvos lkdvos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall definitely a nice set of improvements, I like the interface with the alg_qr, alg_svd, ... different options.

I left some minor comments, and we may consider using the alg=algstruct keywords or not.

Otherwise, given that this is definitely a breaking change, do we want to plan what we want to include in v0.2? It seems like at least the copy_input with alg argument might also be bundled in that release, since that would be breaking. I think the string macros wouldn't be breaking so that could go anywhere inbetween.
I would definitely say that we can leave the CUDA stuff to v0.3

It also looks like the branch is out of date with the main, so maybe you could merge/rebase?

@mtfishman
Copy link
Collaborator Author

Otherwise, given that this is definitely a breaking change, do we want to plan what we want to include in v0.2? It seems like at least the copy_input with alg argument might also be bundled in that release, since that would be breaking. I think the string macros wouldn't be breaking so that could go anywhere inbetween. I would definitely say that we can leave the CUDA stuff to v0.3

That plan sounds good to me. Regarding the string macro, it would be breaking if we decide to delete the aliases and just use string macros to refer to algorithm types. I don't have a very strong opinion on that, just bringing it up as a point of discussion.

It also looks like the branch is out of date with the main, so maybe you could merge/rebase?

I merged, I wasn't able to rebase.

@mtfishman
Copy link
Collaborator Author

Thanks for all of the comments and corrections, I think I addressed all of them.

Copy link
Member

@lkdvos lkdvos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found a typo and small suggestion for where to put a function, but otherwise this looks good to me!

@mtfishman mtfishman mentioned this pull request Apr 24, 2025
4 tasks
@mtfishman mtfishman merged commit 1b44845 into QuantumKitHub:main Apr 24, 2025
9 checks passed
@mtfishman mtfishman deleted the ortho_trunc branch April 24, 2025 19:48
mtfishman referenced this pull request May 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants